﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Text.RegularExpressions;

namespace FortuneHeaven.Web.Controls
{
    public partial class TextPagination : System.Web.UI.UserControl
    {
        private int size = 10;
        private int maxsize = 10;
        public delegate void PagerChangeHandler(object sender, int currentpage);
        public event PagerChangeHandler PagerChanged;
        protected void Page_Load(object sender, EventArgs e)
        {
            string currentstr = Request["page"];
            int tt = 1;
            int.TryParse(currentstr, out tt);
            CurrentPageIndex = tt;
        }

        public string GetNewStr(int page)
        {
            Regex reg = new Regex(@"page=\d*");
            string k = Request.Url.OriginalString;
            Match ma = reg.Match(k);
            if (ma.Success)
            {
                k = reg.Replace(k, "page=" + page.ToString());
            }
            else
            {
                if (Request.QueryString.Count == 0)
                {
                    k = k + "?page=" + page.ToString();
                }
                else
                {
                    k = k + "&page=" + page.ToString();
                }
            }
            return k;
        }
        public void SetLinkStatus()
        {
            StringBuilder buider = new StringBuilder();
            string ori = Request.Url.AbsolutePath;
            int small = 1;
            int big = 1;

            //if (PageCount == 1)
            //{
            //    return;
            //}

            if (PageCount < maxsize)
            {
                small = 1;
                big = PageCount;
            }
            else
            {
                if (CurrentPageIndex <= maxsize / 2)
                {
                    big = maxsize;
                }
                else
                {
                    big = (maxsize / 2 + CurrentPageIndex) > PageCount ? (maxsize / 2 + CurrentPageIndex) : PageCount;
                    small = CurrentPageIndex - maxsize / 2;
                }
            }

            if (CurrentPageIndex != small)
            {
                buider.Append(string.Format("<a href=\"{0}\">上一页</a>", GetNewStr(CurrentPageIndex - 1)));
            }
            for (int index = small; index <= big; index++)
            {
                if (index == CurrentPageIndex)
                {
                    buider.Append(string.Format("<span>{0}</span>", index.ToString()));
                }
                else
                {
                    buider.Append(string.Format("<a href=\"{0}\">{1}</a>", GetNewStr(index), index.ToString()));
                }
            }
            if (CurrentPageIndex != big)
            {
                buider.Append(string.Format("<a href=\"{0}\">下一页</a>", GetNewStr(CurrentPageIndex + 1)));
            }
            sePage.InnerHtml = buider.ToString();
        }

        public int CurrentPageIndex
        {
            get
            {
                object cpage = ViewState["CurrentPageIndex"];
                int pindex = (cpage == null) ? 1 : (int)cpage;
                if (pindex > PageCount && PageCount > 0)
                    return PageCount;
                else if (pindex < 1)
                    return 1;
                return pindex;
            }
            set
            {
                int cpage = value;
                if (cpage < 1)
                    cpage = 1;
                else if (cpage > this.PageCount)
                    cpage = this.PageCount;
                ViewState["CurrentPageIndex"] = cpage;
                SetLinkStatus();
                RaiseEvent();
            }
        }

        public int RecordCount
        {
            get
            {
                object obj = ViewState["Recordcount"];
                return (obj == null) ? 0 : (int)obj;
            }
            set
            {
                ViewState["Recordcount"] = value;
            }
        }

        public int PagesRemain
        {
            get
            {
                return PageCount - CurrentPageIndex;
            }
        }

        public int PageSize
        {
            get
            {
                object obj = ViewState["PageSize"];
                if (obj == null)
                {
                    obj = size;
                }
                return (obj == null) ? size : (int)obj;
            }
            set
            {
                int pageSize = value;

                if (Math.Abs(pageSize) == 0)
                    pageSize = size;

                ViewState["PageSize"] = pageSize;
            }
        }

        public int RecordsRemain
        {
            get
            {
                if (CurrentPageIndex < PageCount)
                {
                    return RecordCount - (CurrentPageIndex * PageSize);
                }
                else
                {
                    return 0;
                }
            }
        }

        public int PageCount
        {
            get { return (RecordCount > 0) ? (int)Math.Ceiling((double)RecordCount / (double)PageSize) : 1; }
        }
        public void RaiseEvent()
        {
            if (PagerChanged != null)
            {
                PagerChanged(this, CurrentPageIndex);
            }
        }
    }
}